// packageStore/pages/purchase/goodslist/goodslist.js
var app = getApp();
import { api} from '../../../../js/util'
import BasePage from '../../../../js/common/base-page'
import { orderdetail} from "../../../../js/modules/order.js"
BasePage({
  pageKey:"orderGoodsList",
  validateLogin: false,
  /**
   * 页面的初始数据
   */
  data: {
    type:1,      //上一个页面标识  0：订单详情 1:拼团/预售订单详情  2：普通确认订单  3：拼团预售确认订单
    list:[],
    quantityDigits:2,//数量显示位数
    priceDigits:2, //价格显示位数
    decimalDigits: 2,//金额小数位数
    imgDomain: api.imgDomain,
    enableOss: api.enableOss,
    Currency:'',//货币符号
    count: { // 合计
      Payables: 0, // 合计金额
      PayableIntegral: 0, // 合计积分
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu();//隐藏分享
    let that = this;
    let pages = getCurrentPages();
    let prevPage = pages[pages.length-2];   //上个页面
    let prevData = prevPage.data;
    let count = {
      Payables: 0,
      PayableIntegral: 0
    }
    let type,list;
    if(prevPage.__route__=='pages/store/purchase/confirm/confirm'){
      type = 2;
      list = prevData.spOder;
      list = list.map((item) => {
        item.Products = item.Products || []
        item.Products.map(product => {
          product.Goods = product.Goods || []
          product.Goods.map((good) => {
            good.Payables = good.AuxPrice * good.AuxQty
            good.PayableIntegral = good.AuxSalesIntegral * good.AuxQty
            count.Payables += good.Payables
            count.PayableIntegral += good.PayableIntegral
            return good
          })
          return product
        })
        return item
      })
    }else if(prevPage.__route__=='packageStore/pages/purchase/detail/detail'){
      type = 0;
      list = prevData.OrderItem;
      let orderInfo = prevData.OrderInfo || {}
      count.Payables = orderInfo.ProductAmount || 0
      count.PayableIntegral = orderInfo.PayableIntegral || 0
    }else if(prevPage.__route__=='packageStore/pages/purchase/groupdetail/groupdetail'){
      type = 1;
      list = prevData.OrderItem;
      list = list.map((item) => {
        item.Payables = item.GroupPrice * item.Quantity
        count.Payables += item.Payables
        return item
      })
      // 团购无积分
      count.PayableIntegral = 0
    }else{
      type = 3;
      list = prevData.Goods;
      list = list.map((item) => {
        item.Payables = item.GroupPrice * item.Quantity
        count.Payables += item.Payables
        return item
      })
      // 团购无积分
      count.PayableIntegral = 0
    }
    this.setData({
      type:type,
      list:list,
      count
    });
  },
  /**
   * 备注改变
   */
  changeRemarks:function(e){
    let {type,idx} = e.currentTarget.dataset;
    let value = e.detail.value;
    if(type==2){
      let {pdx,gdx} = e.currentTarget.dataset;
      let obj = this.data.list[idx].Products[pdx].Goods[gdx];
      obj.BuyRemark = value;
    }else{
      let obj = this.data.list[idx];
      obj.BuyRemark = value;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      Currency:api.Currency || '￥'
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
   
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})